package com.en.sharing.memberclas;

/**
 * 嵌套类测试
 *
 * @author 付晓飞
 * @date 2024/3/12 7:25 下午
 */
public class NestedClassTest {

    public static void main(String[] args) {
        // 静态内部类 可以直接创建实例
        StaticMemberClass.StaticInnerClass staticInnerClass = new StaticMemberClass.StaticInnerClass();
        staticInnerClass.test();

        // 非静态内部类无法直接创建实例，依赖于顶层类的实例
        // NonstaticMemberClass.NonstaticInnerClass nonstaticInnerClass = new NonstaticMemberClass.NonstaticInnerClass();
        NonstaticMemberClass nonstaticMemberClass = new NonstaticMemberClass();
        NonstaticMemberClass.NonstaticInnerClass nonstaticInnerClass = nonstaticMemberClass.new NonstaticInnerClass();
        nonstaticInnerClass.test();

        // 局部类 多次重复加载，可以优化成静态成员类
        new NestedClassTest().localClassTest();
        new NestedClassTest().localClassTest();
    }

    public void localClassTest() {
        class LocalClass {
            {
                System.out.println("local class init");
            }
            public void test() {
                System.out.println("局部类");
            }
        }
        LocalClass localClass = new LocalClass();
        localClass.test();
    }

}
